<?php
namespace Sys\Controller;
use Common\Controller\BaseController;
class TeacherController extends BaseController{
    public function index(){
        $m=M('teachers');
        $res=$m->page(I('get.p'),2)->select();
        $this->assign('teacher',$res);
        $count = $m->count();
        $Page = new \Think\Page($count,2);
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $show = $Page->show();
        $this->assign('page',$show);
        $this->display();
    }
    public function addTeacher(){
        $this->getDict('role');
        $this->display();
    }
    public function doAddTeacher(){
        $m=M('teachers');
        if(!$m-> create($_POST)){
            exit($m->getError());
        }
        if($m->add()){
            $this->success('添加成功',U('./Sys/Teacher/index'));
        }else{
            echo $m->getError();
        }

    }
    public function doEditTeacher(){
        $m=M('teachers');//D(')
        if($m->create()){
            $m->save();
            $res = [
                'msg'=>'编辑成功',
                'error'=>0,
                'data'=>I('post.')
            ];
        }else{
            $res = [
                'msg'=>"编辑失败",
                'error'=>1,

            ];
        }

        $this->ajaxReturn($res);
    }
    public function doDelTeacher(){
        $where=I('post.ids');
        $m=M('teachers');
        if($m->delete($where)){
            $res=[
                'msg'=>'删除成功',
                'error'=>0
            ];
        }else{
            $res=[
                'msg'=>'删除失败',
                'error'=>1
            ];
        }
        $this->ajaxReturn($res);

    }

}
